package com.yalemang.widget.tableview

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.recyclerview.widget.RecyclerView

internal class RecyclerViewAdapter(var adapter: TableAdapter) : RecyclerView.Adapter<RecyclerViewAdapter.YjTableViewHolder>() {

    class YjTableViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var linearLayout: LinearLayout = itemView.findViewById(R.id.linear_layout)
        var viewLineY: View = itemView.findViewById(R.id.view_line_y)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): YjTableViewHolder {
        val itemView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_yj_table_view, parent, false)
        return YjTableViewHolder(itemView)
    }

    override fun getItemCount(): Int {
        return adapter.getYCount() + 1
    }

    private fun getViewLine(context: Context) : View {
        return View(context)
    }

    override fun onBindViewHolder(holder: YjTableViewHolder, yPosition: Int) {
        val xCount = adapter.getXCount() + 1
        val yCount = adapter.getYCount() + 1
        val tableView = adapter.tableView
        holder.linearLayout.removeAllViews()
        if (yPosition == 0) {
            // x轴
            for (xPosition in 0 until xCount) {
                if (xPosition == 0) {
                    // xy轴交界处
                    val params =
                        LinearLayout.LayoutParams(tableView.yWidth, tableView.xHeight)
                    val view = adapter.createXYView(tableView)
                    if (view != null) {
                        view.layoutParams = params
                        holder.linearLayout.addView(view)
                        adapter.showXY(view)
                    }
                } else {
                    val params =
                        LinearLayout.LayoutParams(0, tableView.xHeight)
                    val view = adapter.createXView(tableView, xPosition - 1)
                    params.weight = 1F
                    view.layoutParams = params
                    if (tableView.isOpenLine) {
                        val viewLine = getViewLine(tableView.context)
                        val lineParams =
                            LinearLayout.LayoutParams(tableView.centerLineWidth, LinearLayout.LayoutParams.MATCH_PARENT)
                        viewLine.setBackgroundColor(tableView.centerLineColor)
                        viewLine.layoutParams = lineParams
                        holder.linearLayout.addView(viewLine)
                    }
                    holder.linearLayout.addView(view)
                    adapter.showX(view, xPosition - 1)
                }
            }
        } else {
            for (xPosition in 0 until xCount) {
                if (xPosition == 0) {
                    // y轴交界处
                    val params =
                        LinearLayout.LayoutParams(tableView.yWidth, tableView.yHeight)
                    val view = adapter.createYView(tableView, yPosition - 1)
                    view.layoutParams = params
                    holder.linearLayout.addView(view)
                    adapter.showY(view, yPosition - 1)
                } else {
                    val params =
                        LinearLayout.LayoutParams(0, tableView.yHeight)
                    val view = adapter.createContentView(tableView, xPosition - 1, yPosition - 1)
                    params.weight = 1F
                    view.layoutParams = params
                    if (tableView.isOpenLine) {
                        val viewLine = getViewLine(tableView.context)
                        val lineParams =
                            LinearLayout.LayoutParams(tableView.centerLineWidth, LinearLayout.LayoutParams.MATCH_PARENT)
                        viewLine.setBackgroundColor(tableView.centerLineColor)
                        viewLine.layoutParams = lineParams
                        holder.linearLayout.addView(viewLine)
                    }
                    holder.linearLayout.addView(view)
                    adapter.showContent(view, xPosition - 1, yPosition - 1)
                }
            }
        }

        // 表格网线
        if (tableView.isOpenLine) {
            if (yPosition == yCount - 1) {
                holder.viewLineY.visibility = View.GONE
            } else {
                val params = holder.viewLineY.layoutParams
                params.height = tableView.centerLineWidth
                holder.viewLineY.layoutParams = params
                holder.viewLineY.visibility = View.VISIBLE
                holder.viewLineY.setBackgroundColor(tableView.centerLineColor)
            }
        } else {
            holder.viewLineY.visibility = View.GONE
        }
    }
}